package com.wgchao.authmanager.application.service;

import com.wgchao.authmanager.domain.model.Agency;
import com.wgchao.authmanager.domain.model.Role;
import com.wgchao.authmanager.domain.params.RoleCreator;
import com.wgchao.authmanager.domain.model.RoleIdentity;
import com.wgchao.authmanager.domain.params.RoleUpdator;
import com.wgchao.authmanager.domain.repository.AgencyRepository;
import com.wgchao.authmanager.domain.repository.RoleRepository;
import com.wgchao.authmanager.infrastructure.exception.ObjectNotExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: RoleService
 * @author: wgchao
 * @createTime: 2020/4/26 4:09 PM
 * @Description: 角色信息管理（应用服务）
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleService {
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private AgencyRepository agencyRepository;

    /**
     * 向机构添加角色
     * @param agencyCode
     * @param creator
     * @param operator
     */
    public void addRole(String agencyCode, RoleCreator creator, String operator){
        Agency agency = agencyRepository.get(agencyCode);
        Role role = Role.addRole(agency, creator.getRoleName(), creator.getRemark(), operator);
        roleRepository.add(role);
    }

    /**
     * 修改角色信息
     * @param agencyCode
     * @param roleName
     * @param updator
     * @param operator
     */
    public void resiveRoleInfo(String agencyCode, String roleName, RoleUpdator updator, String operator){
        Role role = roleRepository.get(new RoleIdentity(agencyCode, roleName));
        if(role == null){
            throw new ObjectNotExistException("角色不存在");
        }
        role.resiveInfo(updator, operator);
        roleRepository.add(role);
    }

    /**
     * 停用角色
     * @param agencyCode
     * @param roleName
     * @param operator
     */
    public void stopUsingRole(String agencyCode, String roleName, String operator){
        Role role = roleRepository.get(new RoleIdentity(agencyCode, roleName));
        if(role == null){
            throw new ObjectNotExistException("角色不存在");
        }
        role.stopUsing(operator);
        roleRepository.add(role);
    }

    /**
     * 重新启用角色
     * @param agencyCode
     * @param roleName
     * @param operator
     */
    public void reUseRole(String agencyCode, String roleName, String operator){
        Role role = roleRepository.get(new RoleIdentity(agencyCode, roleName));
        if(role == null){
            throw new ObjectNotExistException("角色不存在");
        }
        role.reEnable(operator);
        roleRepository.add(role);
    }

    /**
     * 删除角色
     * @param agencyCode
     * @param roleName
     * @param operator
     */
    public void removeRole(String agencyCode, String roleName, String operator){
        Role role = roleRepository.get(new RoleIdentity(agencyCode, roleName));
        if(role == null){
            throw new ObjectNotExistException("角色不存在");
        }
        roleRepository.remove(role, operator);
    }
}
